<?php
namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
        //先获取banner图，这个地方做缓存，避免每次请求
        if($index = F('index_data')){
            //再检查是否过期
            if(strtotime($index['expired']) > time()){
                if(empty($index['banner'])){
                    $banner = json_decode($this->http->curl_request(BASE_URL.'banner'),true);
                }else{
                    $banner = $index['banner'];
                }
                if(empty($index['nTop'])) {
                    $nTop = json_decode($this->http->curl_request(BASE_URL . 'ntop'), true);
                }else{
                    $nTop = $index['nTop'];
                }
                if(empty($index['list_news'])){
                    //最后获取天狗的健康信息
                    $news_list = json_decode($this->http->curl_request(C('TIDOG_HOST')),true);
                }else{
                    $news_list = $index['list_news'];
                }
            }else{
                //说明已经过期，重新写入，并更新过期时间
                $banner = json_decode($this->http->curl_request(BASE_URL.'banner'),true);
                $nTop = json_decode($this->http->curl_request(BASE_URL . 'ntop'), true);
                $news_list = json_decode($this->http->curl_request(C('TIDOG_HOST')),true);
                $expired = time() + 5*60;
                F('index_data',['banner'=>$banner,'nTop'=>$nTop,'news_list'=>$news_list,'expired'=>$expired]);
            }
        }else{
            //说明已经过期，重新写入，并更新过期时间
            $banner = json_decode($this->http->curl_request(BASE_URL.'banner'),true);
            $nTop = json_decode($this->http->curl_request(BASE_URL . 'ntop'), true);
            $news_list = json_decode($this->http->curl_request(C('TIDOG_HOST')),true);
            $expired = time() + 5*60;
            F('index_data',['banner'=>$banner,'nTop'=>$nTop,'news_list'=>$news_list,'expired'=>$expired]);
        }
        //请求最新的天狗云健康信息
        //先获取分类信息
        if(!$category_list =F('index_category')){
            $category = json_decode($this->http->curl_request(C('TIDOG_CATEGORY_HOST')),true);
            if($category){
                $i = 1;
                foreach($category['tngou'] as $k => $v ){
                    $category_list[intval($v['id'])]['title'] = $v['title'];
                    $category_list[intval($v['id'])]['class'] = 'know-right-bq'.$v['id'];
                    $i++;
                }
                F('index_category',$category_list);
            }
        }
        //查看是否用户登录信息
        if(is_login()){
            $avatar = $_SESSION['user_login']['avatar'];
            if(!strpos($avatar,':')){
                $avatar = C('IMG_PREFIX').$avatar;
            }
            $this->assign('avatar',$avatar);
        }

        $this->assign('banner',$banner['results']);
        $this->assign('nTop',$nTop['results']);
        $this->assign('category_list',$category_list);
        $this->assign('list_news',$news_list['tngou']);
        $this->assign('tngou_image_prefix',C('TIDOG_IMG_PREFIX'));
        //主页获取各个接口信息
        $this->display('index');
    }
}